<?php

namespace app\api\controller;

use app\api\model\Configs;
use app\api\model\User;
use app\hooks\UserHook;
use think\Request;
use think\Validate;

class Register extends Base
{

    // 是否自动实例化当前模型
    protected $autoNewModel = false;
    // 是否开启自动验证
//    protected $autoValidateCheck = false;

    public function information()
    {
        //整理数据
        $data = [
            'title' => '注册账户',
            'desc' => '注册成功后将自动登录，如有疑问请联系管理员',
            'logo' => get_oss_url('/static/system/logo.png'),
            'formData' => [
                'phone' => '',
                'password' => '',
//                'password_confirm' => '',
//                'yzm' => '',
                'invite_code' => input('invite_code', ''),
            ],
            'yzm' => [
                'default' => '获取验证码',
                'send' => '剩余 {s} 秒'
            ],
            'agreements' => [
                'text' => '我已阅读并同意',
                'lists' => [
                    ['name' => '《用户服务协议》', 'alias' => '', 'params' => []]
                ],
            ],
            'submit' => '立即注册'
        ];
        //响应数据
        return showSuccess($data);
    }


    public function action()
    {
//        dd(request()->param());
        $data = request()->param();
        //验证内容
//        $validate = new Validate();
//        //验证信息
//        if (!$validate->rule([
//            'mobile' => 'mobile',
//            'password' => 'require',
//            'pay_password' => 'require',
////            'password_confirm' => 'require',
//            'yzm' => 'number',
//            'invite_code' => 'require'
//        ])->message([
//            'mobile.mobile' => '无效电话号码',
//            'password.require' => '请输入登录密码',
//            'pay_password' => '请输入支付密码',
////            'password_confirm.require' => '请再次输入密码',
//            'yzm.require' => '请输入密码',
//            'invite_code.require' => '请输入密码',
//        ])->check($data)) {
//            return showError($validate->getError());
//        }

//        if($registerUser = User::where('phone', $data['phone'])->find()){
//            return showError('该手机号已注册，请勿重复注册');
//        }

        //判断密码是否一致
//        if ($data['password'] !== $data['password_confirm']) {
//            return showError('前后密码不一致');
//        }

//        $wnyzm = Configs::where('alias', 'SMS_UNIVERSAL_CODE')->value('content');
//        if((int)($yzm = $data['yzm']) != $wnyzm){
//            $CacheClass = \think\facade\Cache::store(config('cms.user.token.store'));
//            $yzmCache = $CacheClass->get('mobile_'.$data['mobile']);
//            if(is_null($yzmCache)){
//                return showError('验证码已过期，请重新获取');
//            }
//            if((int)$yzm != (int)($yzmCache['code'])){
//                return showError('验证码不正确');
//            }
//        }
//        if (!$addUser['pid'] = (int)\app\api\model\User::where('invite_code', $data['invite_code'])->value('id')) {
//            return showError('邀请码无效');
//        }
//        $addUser['bid'] = 0;
        $p_user = User::where('invite_code', $data['invite_code'])->find();
        $parent_ids = '|' . $p_user['id'] . $p_user['parent_ids'];
        if(empty($p_user['parent_ids'])){
            $parent_ids = '|' . $p_user['id'] . '|';
        }
        $addUser['password'] = password_hash($data['password'], PASSWORD_BCRYPT);
//        $addUser['pay_password'] = md5($data['pay_password']);
        $addUser['invite_code'] = User::uniqueInviteCode();
        $addUser['phone'] = $data['phone'];
        $addUser['avatar']      = '';//\app\api\model\Config::getConfigValue('app_avatar');
        $addUser['nickname']    = '';//\app\api\model\Config::getConfigValue('app_nickname');
        $addUser['parent_ids'] = $parent_ids;
        $addUser['pid'] = $p_user['id'];
        $saveUser = User::create($addUser);
        $user = cms_login([
            'data'=>$saveUser,
            'tag'=>'user'
        ]);
//        UserHook::run($saveUser->id);
        return showSuccess($user);
    }



}